package cn.ex.org.service.impl;

import cn.ex.basic.customexception.BusinessException;
import cn.ex.basic.service.impl.BasicServiceImpl;
import cn.ex.basic.util.MailUtil;
import cn.ex.org.domain.Employee;
import cn.ex.org.domain.Shop;
import cn.ex.org.domain.ShopAuditLog;
import cn.ex.org.mapper.EmployeeMapper;
import cn.ex.org.mapper.ShopAuditLogMapper;
import cn.ex.org.mapper.ShopMapper;
import cn.ex.org.service.IShopService;
import cn.ex.org.vo.EmailVo;
import cn.ex.org.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 实现店铺入驻的实现类
 *
 * @author yaosang
 * @since 2022-06-12
 */
@Service
public class ShopServiceImpl extends BasicServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Override
    @Transactional
    public void settlement(Shop shop) {

        Employee admin = shop.getAdmin();
        //校验参数
        if(StringUtils.isEmpty(shop.getName())||
            StringUtils.isEmpty(shop.getTel())||
            StringUtils.isEmpty(admin)){
            throw new BusinessException("请输入用户信息");
        }
       /* //校验信息
        if(!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("error:字符不合法！！！");
        }*/


        //根据店铺名查询店铺信息
        Shop shopTmp = shopMapper.loadByName(shop.getName());
        //店铺已经被注册
        if(shopTmp != null ){
            throw new BusinessException("店铺已经被注册！！！");
        }
        //查询店主是否注册
        Employee empTmp = employeeMapper.loadByUsername(admin.getUsername());
        if(empTmp != null && empTmp.getShop_id() != null){
            throw new BusinessException("该用户已被注册，是否需要找回密码！！！");
        }
        //新增店主信息
        if(empTmp == null){
            employeeMapper.save(admin);
            empTmp = admin;
        }
        //从刚才新增的店主信息,设置店铺信息
        shop.setAdminId(empTmp.getId());

        //新增店铺，返回自增主键用于给Shop_id赋值
        shopMapper.save(shop);
        //设置Employee的外键setShop_id
        empTmp.setShop_id(shop.getId());
        //修改刚才新增的店主信息
        employeeMapper.update(empTmp);
    }

    /**
     * 人工审核通过
     * @param log
     */
    @Override
    @Transactional
    public void pass(ShopAuditLog log) {
        System.out.println(log);
        //审核通过设置状态
        Shop shopTmp= shopMapper.loadById(log.getShopId());
        shopTmp.setState(1);
        shopMapper.update(shopTmp);
        //新增店铺审核通过日志记录表
        log.setState(1);
        log.setAuditId(333L);
        shopAuditLogMapper.save(log);

        //发送邮件
        Employee boss = employeeMapper.loadByShopId(log.getShopId());
        EmailVo emailVo = new EmailVo();
        emailVo.setEmail(boss.getEmail());
        emailVo.setSubject("宠物乐园-店铺入驻激活请求");
        emailVo.setContent("亲，您的店铺信息已经通过审核，请尽快完成激活并使用:" +
                "激活链接：http://localhost:8080/shop/activation/"+log.getShopId());
        MailUtil.sendMessageMail(emailVo);
    }

    /**
     * 审核驳回
     * @param log
     */
    @Override
    @Transactional
    public void reject(ShopAuditLog log) {
        //审核驳回设置状态
        Shop shop= shopMapper.loadById(log.getShopId());
        shop.setState(-1);
        shopMapper.update(shop);
        //设置审核人,和审核状态
        log.setState(-1);
        log.setAuditId(333L);
        //新增店铺审核驳回日志记录表
        shopAuditLogMapper.save(log);

        //发送邮件
        Employee boss = employeeMapper.loadByShopId(log.getShopId());
        EmailVo emailVo = new EmailVo();
        emailVo.setEmail(boss.getEmail());
        emailVo.setSubject("宠物乐园-店铺入驻驳回任务");
        emailVo.setContent("亲，您的店铺信息有误，审核不通过，请尽快修改并重新提交审核！！修改链接:" +
                "http://localhost:8081/#/register2?shopId="+log.getShopId());
        MailUtil.sendMessageMail(emailVo);
    }

    @Override
    public List<ShopVo> getEcharts() {
        return shopMapper.queryEcharts();
    }

    @Override
    @Transactional
    public void batchSave(List<Shop> shops) {
        shopMapper.batchSave(shops);
    }
}
